package uy.com.dino.samefile.loader;

import uy.com.dino.samefile.model.FileInfo;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;

public class LoaderImpl implements Loader {

    private FileFilter fileFilter;

    private Processor processor;

    public void setFileFilter(FileFilter fileFilter) {
        this.fileFilter = fileFilter;
    }

    public void setProcessor(Processor processor) {
        this.processor = processor;
    }

    @Override
    public List<FileInfo> loadFileInfo(String path) {
        List<FileInfo> fileInfoList = new ArrayList<FileInfo>();
        File initialFolder = new File(path);
        this.loadFileInfo(initialFolder, fileInfoList);
        return fileInfoList;
    }


    private void loadFileInfo(File file, List<FileInfo> fileInfoList) {
        if (file.isDirectory()) {
            File[] files = file.listFiles(fileFilter);
            for (File listedFile : files) {
                loadFileInfo(listedFile, fileInfoList);
            }
        } else {
            FileInfo fileInfo = this.processor.process(file);
            fileInfoList.add(fileInfo);
        }
    }
}
